# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-binaryheap
_rockname=binaryheap
pkgver=0.4
_pkgver=${pkgver%%.*}v${pkgver#*.}
pkgrel=0
pkgdesc="Binary heap implementation in Lua"
url="https://tieske.github.io/binaryheap.lua/"
arch="noarch"
license="MIT"
depends="lua"
checkdepends="lua-busted"
source="https://github.com/Tieske/binaryheap.lua/archive/version_$_pkgver/binaryheap.lua-$pkgver.tar.gz"
builddir="$srcdir/binaryheap.lua-version_$_pkgver"

_luaversions="5.1 5.2 5.3"
for _v in $_luaversions; do
	checkdepends="$checkdepends lua$_v lua$_v-busted"
	provides="$provides lua$_v-${pkgname#lua-}=$pkgver-r$pkgrel"
done

# XXX: luajit is not available on ppc64le, riscv64 and loongarch64.
case "$CARCH" in
ppc64le|riscv64|loongarch64) ;;
*)
	checkdepends="$checkdepends luajit"
	_luaversions="$_luaversions jit"
	;;
esac

prepare() {
	default_prepare
	rm .busted
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Testing on lua$lver"
		lua$lver /usr/bin/busted || {
			# FIXME: Allow failure on Lua 5.3.
			# https://github.com/Tieske/binaryheap.lua/issues/17
			[ $lver = 5.3 ]
		}
	done
}

package() {
	local rockdir="$pkgdir/usr/lib/luarocks/rocks-common/$_rockname/$pkgver-1"

	install -m 644 -D "$builddir"/src/binaryheap.lua \
		"$pkgdir"/usr/share/lua/common/binaryheap.lua

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="2d26108af024f6de5873d9ad2a10dc2fec5b603770c5ff3b3b36289c73261ad6952ed790caad6886bd313a41f9bd42a8257585dd653508d7f1bcd5d1236d2e12  binaryheap.lua-0.4.tar.gz"
